import React, { Component } from 'react'
import SearchArea from 'components/search-area.js';
import ResizeTable from 'components/resize-table'
import {messages} from "share/common";
import AuthorizationHistoryService from './authorization-history.service';
import moment from 'moment';
import config from 'config';
import FileSaver from "file-saver";
export default class AuthorizationHistory extends Component {
  constructor(props) {
    super(props);
    this.state = {
      searchForm: [
        {
          type: 'input',
          key: 'userName',    //必填，唯一，每行的标识
          id: 'userName',
          label: messages('components.key1340'/*姓名/eid*/),
        }
      ],
      loading: true,
      data: [],
      pagination: {
        page: 0,
        total: 0,
        pageSize: 10,
      },
      //搜索关键字
      params: {
        "userName": ""
      },
      columns: [
        {
          title: messages('approve.key33', /* 序号 */),
          dataIndex: 'index',
        },
        {
          title: messages('booking-management.key26', /* 姓名 */),
          dataIndex: 'userName',
        },
        {
          title: messages('components.keyC002', /* EID */),
          dataIndex: 'userEID',
        },
        {
          title: messages('components.key1364', /* 授权日期 */),
          dataIndex: 'authorizationDate',
          render: text => moment(text).format('YYYY-MM-DD')
        },
        {
          title: messages('budget.key37', /* 操作 */),
          dataIndex: 'action',
          render: (text, record) => {
            return <span onClick={() => this.downloadTemplate(record)} style={{ color: '#1890ff', cursor: 'pointer' }}>下载</span>
            // return <a href={record.authorizationURL} download={record.authorizationName} target="_blank">下载</a>
          }
        },
      ],
    };
  };
  componentDidMount() {
    this.getAuthorizationHistroyList();
  }

  getAuthorizationHistroyList = () => {
    const { pagination, params } = this.state;
    this.setState({
      loading: true,
    });
    let item = {
      "userName": params.userName,
      "page": pagination.page,
      "size": pagination.pageSize,
    };
    AuthorizationHistoryService.getAuthorizationHistroyData(item).then(res => {
      console.log("res", res)
      if (res.status == 200) {
        res.data.map((item, index) => {
            item.index = pagination.page * pagination.pageSize + index + 1;
            item.key = item.index;
          });
          this.setState({
            loading: false,
            data: res.data,
            pagination: {
              page: pagination.page,
              pageSize: pagination.pageSize,
              showSizeChanger: true,
              showQuickJumper: true,
              total: Number(res.headers['x-total-count']),
            }
          })
      }
    })
  };

  clearSearchHandle = (values) => {
    let params = {
        userName: ""
    };
    this.setState({
      params
    }, () => {
      this.getAuthorizationHistroyList()
    })
  };
  handleSearch = (values) => {
    this.setState({
      params: values,
      pagination: {
        page: 0,
        total: 0,
        pageSize: 10,
      },
    }, () => {
      this.getAuthorizationHistroyList()
    })
  };
  onChangePager = (pagination, filters, sorter) => {
    this.setState({
      pagination: {
        page: pagination.current - 1,
        pageSize: pagination.pageSize
      }
    }, () => {
      this.getAuthorizationHistroyList();
    })
  };
  downloadTemplate = (record) => {
    AuthorizationHistoryService.downloadData(record.id).then(res => {
      let blob = new Blob([res.data], {type: "file"});
      // FileSaver.saveAs(b, `${record.authorizationName}`);
      const a = document.createElement('a')
      const blobUrl = window.URL.createObjectURL(blob)
      a.download = record.authorizationName
      a.href = blobUrl
      document.body.appendChild(a)
      a.click()
      URL.revokeObjectURL(blobUrl)
      document.body.removeChild(a)
    })
  }
  render() {
    const { searchForm, loading, data, pagination, columns } = this.state;

    return (
      <div className="authorization-histroy-wrap">
        <SearchArea searchForm={searchForm}
                    clearHandle={this.clearSearchHandle}
                    submitHandle={this.handleSearch}/>  
        <div className="table-header">
          <div className="table-header-title">
            {/*共搜索到*条数据*/}
            {messages('common.total',
              {total: `${pagination.total}`})}
          </div>
        </div>
        <ResizeTable
          loading={loading}
          dataSource={data}
          columns={columns}
          pagination={pagination}
          size="middle"
          bordered
          onChange={this.onChangePager}/>
      </div>
    )
  }
}
